﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
using DTO;
using BUS;

namespace GUI
{
    public partial class ThemTP : Form
    {
        public ThemTP()
        {
            InitializeComponent();
        }
         public BindingSource _bdsPB = new BindingSource();

        public void ShowMaster()
        {
            this.dgvThanhPho.AutoGenerateColumns = true;
            this.dgvThanhPho.DataSource = THANHPHO_BUS.SelectThanhPhosAll();
        }
       

        private void btThem_Click_1(object sender, EventArgs e)
        {
            THANHPHO_DTO tp = new THANHPHO_DTO();
            tp.TenTP = this.tbTenTP.Text;
            bool result = THANHPHO_BUS.ThemThanhPho(tp);
            //tbMaTP.ReadOnly = false;
            int maTP = THANHPHO_BUS.SelectThanhPhoByTen(tbTenTP.Text);

            tp.MaTP = maTP;

            if (result == true)
            {
                MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
                tbMaTP.Text = tp.MaTP.ToString();
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose new ThanhPhoID,Please!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ThemTP_Load(object sender, EventArgs e)
        {
            ShowMaster();
        }

        private void dgvThanhPho_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvThanhPho.CurrentRow != null)
            {
                this.tbMaTP.Text = this.dgvThanhPho.CurrentRow.Cells["MaTP"].Value.ToString();
                this.tbTenTP.Text = this.dgvThanhPho.CurrentRow.Cells["TenTP"].Value.ToString();

            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            THANHPHO_DTO tp = new THANHPHO_DTO();

            tp.MaTP = int.Parse(tbMaTP.Text);
            tp.TenTP = this.tbTenTP.Text;
            bool result = THANHPHO_BUS.SuaThanhPho(tp);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btXoa_Click_1(object sender, EventArgs e)
        {
            if (this.dgvThanhPho.CurrentRow != null)
            {
                String maTP = this.dgvThanhPho.CurrentRow.Cells["MaTP"].Value.ToString();
                int ma = int.Parse(maTP);

                DialogResult dr = MessageBox.Show("Do you want to delete MaTP " + maTP + "?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    bool result = THANHPHO_BUS.XoaThanhPho(ma);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMaster();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose one employee,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btTrolai_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
